﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Aga.GestionDeArchivos.Services;
using Aga.GestionDeArchivos.Web.Models.Roles;
using Aga.GestionDeArchivos.Web.Helpers;
using Aga.GestionDeArchivos.Domain.Entities;

namespace Aga.GestionDeArchivos.Web.Controllers
{
    public class RolesController : ControllerBase
    {
        private const int PageSize = 10;

        public RolesController()
        {
            ViewBag.Menu = MenuKey.Admin;
        }

        [CustomAuthorize(Actions = Aga.GestionDeArchivos.Services.Actions.ViewRoles)]
        public ActionResult Index(int? index)
        {
            RoleService service = new RoleService();

            IndexModel model = new IndexModel
            {
                Roles = service.GetPage(index ?? 0, PageSize)
            };

            return View(model);
        }

        [CustomAuthorize(Actions = Aga.GestionDeArchivos.Services.Actions.CreateRoles)]
        public ActionResult Create()
        {
            CreateModel model = new CreateModel();

            return View(model);
        } 

        [HttpPost]
        [CustomAuthorize(Actions = Aga.GestionDeArchivos.Services.Actions.CreateRoles)]
        public ActionResult Create(CreateModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    RoleService service = new RoleService();

                    service.AddNewRole(model.Name, model.Description);

                    return RedirectToAction("Index");
                }
                else
                {
                    return View(model);
                }
            }
            catch (Exception ex)
            {
                return ShowError(ex);
            }
        }

        [CustomAuthorize(Actions = Aga.GestionDeArchivos.Services.Actions.EditRoles)]
        public ActionResult Edit(int id)
        {
            RoleService service = new RoleService();

            Role role = service.GetById(id);

            EditModel model = new EditModel
            {
                Id = role.Id,
                Name = role.Name,
                Description = role.Description
            };

            return View(model);
        }

        [HttpPost]
        [CustomAuthorize(Actions = Aga.GestionDeArchivos.Services.Actions.EditRoles)]
        public ActionResult Edit(EditModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    RoleService service = new RoleService();

                    Role role = service.GetById(model.Id);

                    role.Name = model.Name;
                    role.Description = model.Description;

                    service.Save(role);

                    return RedirectToAction("Index");
                }
                else
                {
                    return View(model);
                }
            }
            catch (Exception ex)
            {
                return ShowError(ex);
            }
        }

        [CustomAuthorize(Actions = Aga.GestionDeArchivos.Services.Actions.DeleteRoles)]
        public ActionResult Delete(int id)
        {
            RoleService service = new RoleService();

            Role role = service.GetById(id);

            DeleteModel model = new DeleteModel
            {
                Id = role.Id,
                Name = role.Name
            };

            return View(model);
        }

        [HttpPost]
        [CustomAuthorize(Actions = Aga.GestionDeArchivos.Services.Actions.DeleteRoles)]
        public ActionResult Delete(DeleteModel model)
        {
            try
            {
                RoleService service = new RoleService();

                service.Delete(model.Id);

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return ShowError(ex);
            }
        }

        [CustomAuthorize(Actions = Aga.GestionDeArchivos.Services.Actions.EditRoles)]
        public ActionResult Actions(int id)
        {
            RoleService service = new RoleService();

            Role role = service.GetById(id);

            ActionsModel model = new ActionsModel
            {
                RoleId = id,
                RoleName = role.Name,
                Actions = new List<RoleAction>(role.Actions)
            };

            return View(model);
 
        }

        [HttpPost]
        [CustomAuthorize(Actions = Aga.GestionDeArchivos.Services.Actions.EditRoles)]
        public ActionResult Actions(ActionsModel model)
        {
            try
            {
                RoleService service = new RoleService();

                Role role = service.GetById(model.RoleId);

                List<int> actions = new List<int>();

                foreach (var action in Enum.GetValues(typeof(Actions)))
                {
                    int actionId = (int)action;

                    if (actionId == 0)
                        continue;

                    string value = Request.Params["chk_" + actionId.ToString()];

                    if (!value.Equals("false"))
                    {
                        actions.Add((int)action);
                    }
                }

                service.UpdateActions(role.Id, actions);

                return RedirectToAction("Edit", new { id = model.RoleId });
            }
            catch (Exception ex)
            {
                return ShowError(ex);
            }
        }
    }
}
